package com.liwei;

import javax.servlet.http.HttpServletRequest;

import org.jmock.Mock;
import org.jmock.MockObjectTestCase;

public class TestMyClassJMock extends MockObjectTestCase {

	private MyClass myclass;
	private Mock mockRequest;
	private HttpServletRequest request;

	public void setUp() {

		myclass = new MyClass();
		// 构造一个Mock对象
		mockRequest = new Mock(HttpServletRequest.class);
		// 根据Mock对象获取一个HttpServletRequest对象
		request = (HttpServletRequest) mockRequest.proxy();
	}

	public void tearDown() {
	}

	public void testGetName() {
		// 设置要执行的操作，以下设置表示要调用一次HttpServletRequest对象的
		// getParameter方法，传递的参数是"name",期望的返回是"kongxx"
		mockRequest.expects(atLeastOnce()).method("getParameter").with(eq("name")).will(returnValue("kongxx"));
		assertNotNull(myclass.getName(request));
		assertEquals("kongxx", myclass.getName(request));
	}

	public void testGetPassword() {

		// 设置要执行的操作，以下设置表示要调用一次HttpServletRequest对象的
		// getParameter方法，传递的参数是"password",期望的返回是"123456789"
		mockRequest.expects(atLeastOnce()).method("getParameter").with(eq("password")).will(returnValue("123456789"));
		assertNotNull(myclass.getPassword(request));
		assertEquals("123456789", myclass.getPassword(request));
	}

}